package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.common.Assert;
import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.pojo.bo.system.CateBO;
import com.lwc.cfdns.pojo.bo.system.DeptBO;
import com.lwc.cfdns.pojo.dto.system.CateDTO;
import com.lwc.cfdns.pojo.entity.SystemCate;
import com.lwc.cfdns.pojo.entity.SystemDept;
import com.lwc.cfdns.pojo.entity.SystemDict;
import com.lwc.cfdns.pojo.entity.SystemRole;
import com.lwc.cfdns.pojo.vo.system.CateTreeVO;
import com.lwc.cfdns.pojo.vo.system.CateVO;
import com.lwc.cfdns.pojo.vo.system.DeptTreeVO;
import com.lwc.cfdns.service.SystemCateService;
import com.lwc.cfdns.mapper.SystemCateMapper;
import com.lwc.cfdns.utils.ConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author spring
 * @description 针对表【cfdns_system_cate(后台通用分类)】的数据库操作Service实现
 * @createDate 2023-05-30 22:30:14
 */
@Service
public class SystemCateServiceImpl extends ServiceImpl<SystemCateMapper, SystemCate>
        implements SystemCateService {

    /**
     * 创建
     *
     * @param cateDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 22:57
     */
    @Override
    public Map<String, Object> create(CateDTO cateDTO) {
        Map<String, Object> map = new HashMap<>();
        SystemCate systemCate = new SystemCate();
        BeanUtils.copyProperties(cateDTO, systemCate);
        this.baseMapper.insert(systemCate);
        map.put("id", systemCate.getId());
        return map;
    }

    /**
     * 修改
     *
     * @param cateDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 23:00
     */
    @Override
    public Map<String, Object> update(CateDTO cateDTO) {
        HashMap<String, Object> map = new HashMap<>();
        SystemCate systemCate = this.baseMapper.selectById(cateDTO.getId());
        Assert.notNull(systemCate, ResultCode.NOT_DATA_FOUNT);
        BeanUtils.copyProperties(cateDTO, systemCate, "created_at");
        int i = this.baseMapper.updateById(systemCate);
        map.put("result", i);
        return map;
    }

    /**
     * 详情
     *
     * @param id
     * @return CateVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 23:02
     */
    @Override
    public CateVO view(Long id) {
        SystemCate systemCate = this.baseMapper.selectById(id);
        Assert.notNull(systemCate, ResultCode.NOT_DATA_FOUNT);
        CateVO cateVO = new CateVO();
        BeanUtils.copyProperties(systemCate, cateVO);
        return cateVO;
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 23:03
     */
    @Override
    public Map<String, Object> delete(String ids) {
        int i = 0;
        if (ids.contains(",")) {
            i = this.baseMapper.delete(new LambdaQueryWrapper<SystemCate>().in(SystemCate::getId, ConvertUtils.toLongArray(ids)));
        } else {
            i = this.baseMapper.deleteById(ConvertUtils.toLong(ids));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 列表
     *
     * @param cateBO
     * @return IPage<SystemCate>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 23:05
     */
    @Override
    public IPage<SystemCate> lists(CateBO cateBO) {
        Page<SystemCate> page = this.baseMapper.selectPage(cateBO.toPage(), cateBO.toQueryWrapper());

        //转化为VO
        page.convert(s -> {
            CateVO cateVO = new CateVO();
            BeanUtils.copyProperties(s, cateVO);
            return cateVO;
        });

        return page;
    }

    /**
     * 树
     *
     * @param cateBO
     * @return List<CateTreeVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 23:22
     */
    @Override
    public List<CateTreeVO> tree(CateBO cateBO) {
        List<CateTreeVO> tree = new ArrayList<>();
        List<SystemCate> systemCates = this.baseMapper.selectList(cateBO.toQueryWrapper());
        for (SystemCate systemCate : systemCates) {
            if (systemCate.getPid().equals(0L)) {
                CateTreeVO cateTreeVO = new CateTreeVO();
                BeanUtils.copyProperties(systemCate, cateTreeVO);
                cateTreeVO.setLabel(systemCate.getName());
                tree.add(findChildren(systemCates, cateTreeVO));
            }
        }
        return tree;
    }

    /**
     * 递规查找子
     *
     * @param systemCates
     * @param cateTreeVO
     * @return T
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 23:31
     */
    private <T extends CateTreeVO> T findChildren(List<SystemCate> systemCates, T cateTreeVO) {
        for (SystemCate systemCate : systemCates) {
            if (cateTreeVO.getId().equals(systemCate.getPid())) {
                CateTreeVO cateTreeVO1 = new CateTreeVO();
                BeanUtils.copyProperties(systemCate, cateTreeVO1);
                cateTreeVO1.setLabel(systemCate.getName());

                cateTreeVO.getChildren().add(findChildren(systemCates, cateTreeVO1));
            }
        }
        return cateTreeVO;
    }
}




